<template>
  <div class="flex flex-col h-full">
    <a-card>
      <div class="flex justify-between items-center">
        <a-button type="primary" class="mr-4" @click="router.go(-1)"> 返回 </a-button>
        <a-descriptions class="mt-3" :column="4">
          <a-descriptions-item label="仪器类型"> {{ formState?.instrumentTypeName || '-' }}</a-descriptions-item>
          <a-descriptions-item label="样本类型"> {{ formState?.sampleType1Name + formState?.sampleType2Name || '-' }}</a-descriptions-item>
          <a-descriptions-item label="样本前缀">{{ formState?.startNo || '-' }}</a-descriptions-item>
          <a-descriptions-item label="编码格式"> {{ formState?.encodingFormatName || '-' }}</a-descriptions-item>
          <a-descriptions-item label="通讯模式"> {{ formState?.communicationModeName || '-' }}</a-descriptions-item>
          <a-descriptions-item label="版本号">{{ formState?.versionNumber || '-' }}</a-descriptions-item>
          <a-descriptions-item label="备注"> {{ formState?.memo || '-' }}</a-descriptions-item>
        </a-descriptions>
      </div>
    </a-card>
    <a-card class="mt-2 flex-1">
      <div class="flex">
        <a-button class="mb-2" type="primary" @click="onClickPut()"> 新增 </a-button>
      </div>
      <BaseTable :data-source="tableData" column-code="000191" :loading="loading" fill :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'itemType'">
            <span v-if="record.itemType === '1'">数值型</span>
            <span v-if="record.itemType === '2'">文字型</span>
            <span v-if="record.itemType === '3'">阴阳型</span>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onClickPut(record)"> 修改 </a-button>
            <a-divider type="vertical" />
            <BaseButton confirm-text="确认删除？" type="text" danger size="small" @click="onDelete(record)">删除</BaseButton>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <ChecklistAdd v-model:open="editOpen" :row-data="rowData" @success="getData" />
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { message } from 'ant-design-vue'
import BaseTable from '@/components/BaseTable'
import { post } from '@/utils/request'
import ChecklistAdd from './components/ChecklistAdd.vue'
// import BaseButton from '@/components/BaseButton'

const formState = ref({})
const rowData = ref({})
const editOpen = ref(false)
const route = useRoute()
const router = useRouter()

const { loading, tableData, getData, pagination } = useTable({
  url: '/lis/InstrumentType/getLisDictItemList',
  immediate: true,
  formatParams: () => {
    return {
      instrumentTypeId: route.query?.id
    }
  }
})

// 获取信息
const getInstrumentType = async () => {
  const { code, data, msg } = await post('/lis/InstrumentType/getInstrumentType', { id: route.query?.id })
  if (code !== 0) return message.error(msg)
  formState.value = data
}

const onClickPut = (row) => {
  rowData.value = { ...row, instrumentTypeId: route.query?.id }
  editOpen.value = true
}

// 删除
const onDelete = async (row) => {
  const { code, msg } = await post('/lis/InstrumentType/doDelLisItem', row)
  if (code !== 0) return message.error(msg)
  message.success(msg)
  getData()
}

onMounted(() => {
  getInstrumentType()
})
</script>
<style lang="less" scoped>
:deep(.ant-card-head) {
  padding-left: 8px;
}
</style>
